<?php

namespace App\Containers\AppSection\Qualification\Actions;

use App\Containers\AppSection\Confirmation\Tasks\DeleteAllConfirmsByValueTask;
use App\Containers\AppSection\Confirmation\Tasks\FindConfirmationByCodeTask;
use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Profile\Tasks\UpdateProfileTask;
use App\Containers\AppSection\Qualification\Models\Qualification;
use App\Containers\AppSection\Qualification\Tasks\SendToQualificationTask;
use App\Containers\AppSection\Qualification\UI\API\Requests\SendToQualificationRequest;
use App\Ship\Parents\Actions\Action;

class SendToQualificationAction extends Action
{
    public function run(SendToQualificationRequest $request): Qualification
    {
        //Поиск кода
        app(FindConfirmationByCodeTask::class)->run(
            $request->getInputByKey('code'), $request->profile()->phone,
            config('appSection-confirmation.confirms.phone.requests.qualification'),
            config('appSection-confirmation.confirms.phone.type')
        );

        $qualification = app(SendToQualificationTask::class)->run(
            [
                'profile_id' => $request->profile()->id,
                'stage' => 'moderator',
                'conditions' => [
                    'guarantee' => $request->getInputByKey('guarantee'),
                    'conditions' => $request->getInputByKey('conditions'),
                ],
                'status_key' => 'qualification_checking',
            ]
        );


        app(UpdateProfileTask::class)->run($request->profile()->id, [
            'qualification_id' => $qualification->id,
            'qualification_status_key' => 'qualification_checking',
        ]);


        //Удаление отправленных кодов на телефон
        app(DeleteAllConfirmsByValueTask::class)
            ->run($request->profile()->phone, config('appSection-confirmation.confirms.phone.requests.qualification'), config('appSection-confirmation.confirms.phone.type'));


        app(CreateLogTask::class)->run($request->profile()->id, 'Отправка на проверку квалификации инвестора', 'qualification', $qualification->id);

        return $qualification;
    }
}
